<?php

namespace App\Admin\Actions\Admin;

use App\Models\DomainList;
use Encore\Admin\Actions\Action;
use Illuminate\Http\Request;

class BatchAddDomain extends Action
{
    protected $selector = '.batch-add-domain';

    public function handle(Request $request)
    {
        $domains = explode("\r\n",$request->domain_list);
        $sort = $request->sort;
        foreach ($domains as $domain){
            DomainList::query()->create([
                'domain' => $domain,
                'sort' => $sort
            ]);
        }
        return $this->response()->success('批量添加成功')->refresh();
    }

    public function html()
    {
        return <<<HTML
        <a class="btn btn-sm btn-default batch-add-domain">批量添加</a>
HTML;
    }

    // 上传表单
    public function form()
    {
        $this->integer('sort', '排序')->help('越小越靠前');
        $this->textarea('domain_list', '域名列表')->help('一行填入一个域名');
    }
}
